// LoginDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "LoginDlg.h"


// LoginDlg dialog

IMPLEMENT_DYNAMIC(LoginDlg, CPropertyPage)

LoginDlg::LoginDlg()
	: CPropertyPage(LoginDlg::IDD)
{

	EnableAutomation();
	
}

LoginDlg::~LoginDlg()
{
}

void LoginDlg::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CPropertyPage::OnFinalRelease();
}

void LoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_USERNAME, m_userNameLb);
	DDX_Control(pDX, IDC_PASSWORD, m_passWordLb);
	DDX_Control(pDX, IDC_USNAME_TXT, m_userNameEdit);
	DDX_Control(pDX, IDC_PASS_TXT, m_passWordEdit);
}


BEGIN_MESSAGE_MAP(LoginDlg, CPropertyPage)
	ON_BN_CLICKED(IDC_LOGIN_BT, &LoginDlg::OnBnClickedLoginBt)
	ON_BN_CLICKED(IDC_REGISTER_BT, &LoginDlg::OnBnClickedRegisterBt)
	ON_WM_CREATE()
	ON_EN_SETFOCUS(IDC_PASS_TXT, &LoginDlg::OnEnSetfocusPassTxt)
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(LoginDlg, CPropertyPage)
END_DISPATCH_MAP()

// Note: we add support for IID_ILoginDlg to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .IDL file.

// {1A21A4DD-2883-48AD-989B-F3502235934B}
static const IID IID_ILoginDlg =
{ 0x1A21A4DD, 0x2883, 0x48AD, { 0x98, 0x9B, 0xF3, 0x50, 0x22, 0x35, 0x93, 0x4B } };

BEGIN_INTERFACE_MAP(LoginDlg, CPropertyPage)
	INTERFACE_PART(LoginDlg, IID_ILoginDlg, Dispatch)
END_INTERFACE_MAP()


// LoginDlg message handlers

void LoginDlg::OnBnClickedLoginBt()
{
	// TODO: Add your control notification handler code here

	CString name;
	CString passWord;

	m_userNameEdit.GetWindowText(name);
	m_passWordEdit.GetWindowText(passWord);

	CClientApp::getUser()->UserName(name);
	CClientApp::getUser()->PassWord(passWord);
	
	if (!checkUser())
	{
		return;
	}
	
	if (!MySocket::getIntance()->ServerConnect())
	{
		return;
	}

	char* iUser = new char[20];
	char* iPass = new char[13];

	iUser=CHelper::ConverToChar(name,iUser);
	iPass=CHelper::ConverToChar(passWord,iPass);

	CMessage iMes;
	iMes.CreateLogin(iUser,iPass);
	MySocket::getIntance()->SendMessToServer(iMes);
}

void LoginDlg::OnBnClickedRegisterBt()
{
	// TODO: Add your control notification handler code here
}

int LoginDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CPropertyPage::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	
	return 0;
}

void LoginDlg::OnEnSetfocusPassTxt()
{
	m_passWordEdit.SendMessage(EM_SETPASSWORDCHAR, '*', 0);
	// TODO: Add your control notification handler code here
}

bool LoginDlg::checkUser()
{
	if (CClientApp::getUser()->UserName()=="" || CClientApp::getUser()->PassWord()=="")
	{
		return false;
	}
	return true;
}
